package com.vf.cloud.server.colony.util;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.vf.cloud.common.domain.Client;
import com.vf.cloud.server.colony.pool.NodePool;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class ColonyUtil {

	public static void disconnected(String channelId) {
		NodePool.removeNode(channelId);
	}

	public static void sendPing(ChannelHandlerContext ctx, long time) {
		Map<String, Object> clientConfig = new HashMap<String, Object>();
		clientConfig.put("type", "pong");
		clientConfig.put("time", time);
		send(ctx, JsonKit.toJson(clientConfig));
	}

	public static void sendSuccess(ChannelHandlerContext ctx, String messageId) {
		Map<String, Object> success = new HashMap<String, Object>();
		success.put("type", "event");

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("func_name", "OnSuccess");
		data.put("message_id", messageId);

		success.put("data", data);

		send(ctx, JsonKit.toJson(success));
	}

	public static void sendFail(ChannelHandlerContext ctx, String messageId, String message) {
		Map<String, Object> failed = new HashMap<String, Object>();
		failed.put("type", "event");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("func_name", "OnFailed");
		data.put("message_id", messageId);
		data.put("message", message);
		failed.put("data", data);
		send(ctx, JsonKit.toJson(failed));
	}

	public static void send(ChannelHandlerContext ctx, String json) {
		if (ctx != null) {
			ctx.channel().writeAndFlush(new TextWebSocketFrame(json)).addListener(new ChannelFutureListener() {
				@Override
				public void operationComplete(ChannelFuture future) throws Exception {
					if (future.isSuccess()) {
					} else {
					}
				}
			});
		}
	}

	public static void send(ChannelHandlerContext ctx, String json, String messageId) {
		if (ctx != null) {
			ctx.channel().writeAndFlush(new TextWebSocketFrame(json)).addListener(new ChannelFutureListener() {
				@Override
				public void operationComplete(ChannelFuture future) throws Exception {
					if (future.isSuccess()) {
						sendSuccess(ctx, messageId);
					} else {
					}
				}
			});
		}
	}

	/**
	 * 处理同步消息
	 * 
	 * @param rawMsg
	 */
	public static void handleSyn(ChannelHandlerContext ctx, JSONObject rawMsg) {
	}

	public static void handleGPUState(ChannelHandlerContext ctx, JSONObject rawMsg) {
		
		
		
	
		
		
		
		JSONObject obj = rawMsg.getJSONObject("data");
		String nodePwd = obj.getString("localPwd");
		if (StrKit.isBlank(nodePwd)) {
			sendFail(ctx, rawMsg.getString("messageId"), "消息id必填.");
			return;
		}
		Client client = Client.dao.findById(nodePwd);
		if (client == null) {
			client = new Client();
			client.setId(nodePwd);
			client.setName(obj.getString("localIp"));
			client.setType("0");
			client.setPassword(nodePwd);
			client.setIp(obj.getString("localIp"));
			client.setPort(obj.getString("localPort"));
			client.setLimit(obj.getInteger("limit"));
			if (client.save()) {
				sendSuccess(ctx, rawMsg.getString("messageId"));
			} else {
				sendFail(ctx, rawMsg.getString("messageId"), "同步失败.");
			}
			return;
		}

		client.setName(obj.getString("localIp"));
		client.setType("0");
		client.setPassword(nodePwd);
		client.setIp(obj.getString("localIp"));
		client.setPort(obj.getString("localPort"));
		client.setLimit(obj.getInteger("limit"));
		if (client.update()) {
			sendSuccess(ctx, rawMsg.getString("messageId"));
		} else {
			sendFail(ctx, rawMsg.getString("messageId"), "同步失败.");
		}

	}

}
